import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPool {
    private BlockingQueue<Runnable> queue = null;

    public MyThreadPool(int n) {
        queue = new LinkedBlockingQueue<>(1000);
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                try {
                    while(true) {
                        Runnable task = queue.take();
                        task.run();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            //t.setDaemon(true);
            t.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }

}

public class demo17 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool threadPool = new MyThreadPool(10);
        int n = 0;
        while(n < 1000) {
            int i = n;
            threadPool.submit(()->{
                System.out.println("打印元素: " + i);
            });
            n++;
        }
//        for (int i = 0; i < 100; i++) {
//            int id = i;
//            threadPool.submit(() -> {
//                System.out.println(Thread.currentThread().getName() + " id=" + id);
//            });
//        }
    }
}
